home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / mfkasm.zip / PM-TMPLT.ASM < prev    next >
Assembly Source File  |  1993-06-01  |  13KB  |  337 lines

  1. ;---------------------------------------------------------------------------;
  2. ; M.F.Kaplon  Begun:Fri  06-12-1992  Revised:Tue  07-21-1992
  3. ; Title : pm-tmplt.asm
  4. ;
  5. ; This is a sort of bare bones template of a Presentation Manager program
  6. ; It also demonstrates setting up a HOOK for theWM_CHAR message
  7. ;
  8. ;
  9. ; INITIALIZES,CREATES MESSAGE QUEUE, REGISTERS CLASS,CREATES STANDARD WINDOW
  10. ; ESTABLISHES MAIN MESSAGE LOOP, ESTABLISHES BASIC MAIN WINDOW PROCEDURE :
  11. ;
  12. ; MainWinProc  which has 'switches' recognizing WM_CHAR,WM_PAINT and
  13. ; WM_CREATE calling respectively MainKeyBoard, MainPaint, InitMainWindow
  14. ;
  15. ; The user may add additional switches recognizing other WM_... and must
  16. ; then establish the procedures to respond to that call.
  17. ;
  18. ; For System Function Calls
  19. ;
  20. ; 1 - Must use the C Calling Convention which means
  21. ;     Arguments Pushed on Stack in Reverse Order than declared in function
  22. ; 2 - Using .MODEL FLAT when using a pointer, all you have to do is push
  23. ;     the offset of the variable since in FLAT model everyting is NEAR
  24. ;     in the 32 bit space
  25. ; 3 - Uses a 16K stack
  26. ;
  27. ; cmd file to assemble and link is named mlc-w386
  28. ; and should be called as   :  mlc-w386 filename
  29. ; The cmd file assumes that "filename" is in d:\os2_asm
  30. ; Content of mlc-link386
  31. ;
  32. ; ml /c d:\os2_asm\%1.asm
  33. ; IF errorlevel 1  goto errexit
  34. ; link386 /PM:PM /ALIGN:16 /E %1.obj ;
  35. ; del *.obj
  36. ; quit
  37. ; :errexit
  38. ; Echo Compile Error NO Link
  39. ;
  40. ; My source files are in D:\os2_asm and I make my calls from the C:
  41. ; root directory so that the EXE files and the OBJ file are in C:\
  42. ; and the EXE file has same name as the source file.
  43. ; If you are assembling and linking more than one then you have to
  44. ; make appropriate changes. The above does not make a map or list file
  45. ;
  46. ; I get one warning message
  47. ;
  48. ; LINK : warning L4036: no automatic data segment
  49. ;
  50. ; This message has to do with no DGROUP being defined
  51. ; It can be suppressed with DATA NONE in a "DEF" file
  52. ;
  53. ;---------------------------------------------------------------------------;
  54. ;
  55. ;!!!!!!!!!!!!!!  PM PROGRAMS MUST BE LINKED WITH /PM:PM !!!!!!!!!!!!!!
  56. ;!!!!!!!!!     But then they Display ONLY THROUGH PM CALLS   !!!!!!!!!
  57. ;---------------------------------------------------------------------------;
  58. ;
  59.  
  60. .386             ;preceeding .MODEL makes USE32 default
  61. .MODEL           FLAT,SYSCALL,OS_OS2
  62. ;---------- Conditionally required equates -------------
  63. NUMBUFS             equ  1     ; Uncomment if need number routines
  64. ;
  65. INCL_WINERRORS      equ  1
  66. INCL_WIN            equ  1
  67.  
  68. INCLUDE        c:\toolkt20\asm\os2inc\os2def.inc ;structure defns includes POINTL
  69. INCLUDE        c:\toolkt20\asm\os2inc\pmwin.inc  ;structure defns POINTL defn required
  70. INCLUDE        c:\toolkt20\asm\os2inc\pmgpi.inc  ;graphics
  71. INCLUDE        c:\toolkt20\asm\os2inc\pmerr.inc  ;errors
  72. INCLUDE        c:\toolkt20\asm\os2inc\pmshl.inc
  73. INCLUDELIB     c:\toolkt20\os2lib\os2386.lib     ;Library
  74.  
  75. INCLUDE        d:\os2_asm\equs386.inc       ;equates : MFK
  76. INCLUDE        d:\os2_asm\386_dos.mac       ;macros using DOS calls : MFK
  77. INCLUDE        d:\os2_asm\386_win.mac       ;macros using WIN/GR calls :MFK
  78.  
  79. ;---------- Prototype Definitions for MASM 6.0 -------------
  80. InitMainWindow Proto Near SYSCALL var1:DWORD,var2:DWORD,var3:DWORD
  81. MainPaint      Proto Near SYSCALL var1:DWORD
  82. MainKeyBoard   Proto Near SYSCALL var1:DWORD,var2:DWORD,var3:DWORD
  83.  
  84.  
  85. .STACK    8096   ;8K stack
  86.  
  87. .DATA
  88.  
  89. IFDEF NUMBUFS                      ;To use  UNCOMMENT NUMBUFS equate above
  90.   $DefineNumBufs
  91. ENDIF
  92. ;------------- handles --------
  93. hab            DWORD   ?           ;Anchor block Handle
  94. hmq            DWORD   ?           ;Message Queue Handle
  95. hwndMainFrame  DWORD   ?           ;Handle to Main Frame Window
  96. hwndMain       DWORD   ?           ;Handle to Client window
  97. hps            DWORD   ?           ;Presentation Space Handle
  98.  
  99. ;------------- Threads ----------
  100. ThreadID       DWORD   ?           ;ThreadID
  101. ThreadArg      DWORD   ?           ;ARgument to be passed to Thread
  102.  
  103. ;------------- Return Values --------
  104. TorF           DWORD   ?           ;return value, True or False
  105. PaintRet       DWORD   ?           ;return value from Paint Procedure
  106. RetValue       DWORD   ?           ;return values from calls
  107.  
  108. ;------------- Text Strings --------
  109. szAppName       BYTE   "Main",0    ;Class Name of Window Procedure
  110. szWinTitle      BYTE   "*** TSR ***",0 ;Window Title
  111. msgInitErr      BYTE   "!!! This is in a Message Box !!!",0
  112. textMsg         BYTE   "*** Presentation Manager TSR ***",0
  113.  
  114.  
  115. ;------------- Text Strings for WinMessageBox calls --------
  116. msgWinChangeSwitchEntry BYTE " : WinChangeSwitchEntry",0
  117. msgWinQuerySwitchList   BYTE " : WinQuerySwitchList",0
  118. msgWinReleaseHook       BYTE " : WinReleaseHook",0
  119. msgWinSetHook           BYTE " : WinSetHook",0
  120.  
  121. msgDosAllocMem          BYTE " : DosallocMem",0
  122. msgDosCreateThread      BYTE " : DosCreateThread",0
  123. msgDosDevIOCtl          BYTE " : DosDevIOCtl",0
  124. msgDosFreeMem           BYTE " : DosFreeMem",0
  125. msgDosFreeModule        BYTE " : DosFreeModule",0
  126. msgDosKillThread        BYTE " : DosKillThread",0
  127. msgDosLoadModule        BYTE " : DosLoadModule",0
  128. msgDosOpen              BYTE " : DosOpen",0
  129. msgDosQueryProcAddr     BYTE " : DosQueryProcAddr",0
  130.  
  131. ;------------- Styles --------
  132. msgBoxStyle    DWORD   (MB_YESNO OR MB_DEFBUTTON1)
  133. flStyle        DWORD   (CS_SIZEREDRAW OR CS_HITTEST)  ;Window Style
  134. flCtlData      DWORD   (((((FCF_SYSMENU        OR \   ;Window Control Styles
  135.                             FCF_MINMAX)        OR \
  136.                             FCF_SHELLPOSITION) OR \
  137.                             FCF_TASKLIST)      OR \
  138.                             FCF_SIZEBORDER)    OR \
  139.                             FCF_TITLEBAR )
  140.  
  141. ;------------- structures --------
  142. quemsg          QMSG    {,,,,,,}   ;Queue message structure
  143. rect           RECTL   {,,,}       ;Rectangle structure
  144.  
  145. ;-------------- DosOpen -----------------
  146. DevId           BYTE   "KBD$",0     ;Input  Keyboard Device
  147. DevHandle      DWORD   ?            ;Output Device Handle
  148. DevActionTaken DWORD   ?            ;Output Action Taken
  149. DevSize        DWORD   0            ;Input  Logical Size 0 for devuces
  150. DevAttribute   DWORD   0            ;Input  Attribute bits
  151. DevDopenflag   DWORD   1            ;Input  open flag for KBD
  152. DevOpenMode    DWORD   0042h        ;Input  open mode for KBD
  153. DevExtaAttr    DWORD   0            ;Input  extended attributes for KBD
  154.  
  155. ;-------------- DosDevIOCtl -------------
  156. DevCategory    DWORD        ?        ;Input Category
  157.  
  158. ;------------- Miscellaneous --------
  159. parm1          DWORD   ?           ;handle of window sending message
  160. parm2          DWORD   ?           ;message id value
  161. parm3          DWORD   ?           ;message mp1
  162. parm4          DWORD   ?           ;message mp2
  163. textPos        DWORD   ?           ;Position of text on screen
  164. pErrorInfo     DWORD   ?           ;address of ErrorInfo Structure
  165. Concanted       BYTE 64 dup(0)     ;buffer to hold concanted strings
  166.  
  167. ;------------ Specific to TSR Switch List use -----
  168. DllLoadError    BYTE  100 dup(0)   ;Buffer for name of object contributing to error
  169. DllHandle      DWORD ?             ;Handle of Dynamic LInk Module returned here
  170. DllModuleName   BYTE  "TSR_HOOK",0 ; module name
  171. DllProcName     BYTE  "INPUTHOOK",0 ;procedure name in dynamic link module
  172. DllProcAddr    DWORD ?             ;address of proc in dynamic link module
  173.  
  174.  
  175. .CODE
  176.  
  177. startup:                         ;need to do this way with flat model
  178.  
  179. ;-----Initialize Window -Anchor block handle returned = hab
  180. $WinInitialize 0                 ;called with argument 0
  181. mov    hab,eax                   ;return value
  182. .IF hab == NULL
  183.     $DosBeep 1000,500
  184.     $DosExit
  185. .ENDIF
  186.  
  187. ;-----Create MessageQue  QueueHandle returned = hmq
  188. $WinCreateMsgQueue hab,0         ; 0 is default size of queue
  189. mov      hmq,eax                 ;returned queue handle
  190. .IF hmq == NULL
  191.    $DosBeep 600,500
  192.    $WinTerminate   hab
  193. .ENDIF
  194.  
  195. ;---- Register Window Class Returned value is TRUE or FALSE
  196. $WinRegisterClass  hab,offset szAppName,offset MainWinProc,flStyle,0
  197. mov      TorF,eax                ;return value
  198. .IF TorF == FALSE
  199.     $DosBeep  100,500
  200.     $WinTerminate hab
  201. .ENDIF
  202.  
  203. ;---- CreateStandard Window - Returns handle for Main Window Frame and client Window
  204. $WinCreateStdWindow HWND_DESKTOP,WS_VISIBLE,offset flCtlData,offset szAppName,\
  205.                     offset szWinTitle,WS_VISIBLE,0,0,offset hwndMain
  206. mov   hwndMainFrame,eax          ;returned Frame Window handle
  207. .IF hwndMainFrame == 0
  208.    $DosBeep 1000,2000
  209.    $WinDestroyMsgQueue  hmq
  210.    $WinTerminate hab
  211. .ENDIF
  212.  
  213. ;The way to proceed is to install a System Hook. This has to go into a
  214. ;DLL otherwise it cannot be called by other programs. The procedure in
  215. ;the tsr_hook.DLL will inspect WM_CHAR to see if the HOT key is hit and
  216. ;in the test instance will just sound an alarm or something like that to
  217. ;show it is working. In general, when it is HOOKED it will do what the
  218. ;thing is supposed to do.
  219. ;The Journal-Record hook function has the form
  220. ;  VOID APIENTRY JournalRecordHook(HAB hab, PQMSG pQmsg)
  221. ;  and does not return a value.
  222. ;  The function will test only for WM_CHAR and then do its thing.
  223. ;  It could call an application specific function in this tsr.exe
  224. $DosLoadModule  offset DllLoadError,LENGTHOF DllLoadError,offset DllModuleName,offset DllHandle
  225. $DosQueryProcAddr  DllHandle,1,0,offset DllProcAddr
  226. $WinSetHook  hab,NULLHANDLE,HK_INPUT,[DllProcAddr],DllHandle
  227.  
  228. ;-------- MainMessageLoop ------------
  229. mml: $WinGetMsg hab,offset quemsg,0,0,0
  230.      mov   TorF,eax    ;return value
  231.      .IF TorF == TRUE
  232.          $WinDispatchMsg hab,offset quemsg
  233.          jmp   mml
  234.      .ENDIF
  235.  
  236. ;--------- Exit Code -------
  237. $WinReleaseHook  hab,NULL,HK_INPUT,[DllProcAddr],DllHandle
  238. $DosFreeModule DllHandle
  239. $WinDestroyWindow  hwndMainFrame
  240. $WinDestroyMsgQueue  hmq
  241. $WinTerminate hab
  242.  
  243. ;------------------ MainWinProc -----------------
  244. ;parm1 = hwnd,parm2 = msg,parm3  = mp1,parm4 = mp2
  245. ;this is called from System and so has to do everything itself
  246. MainWinProc Proc Near
  247.     ;----------- Get Passed Parameters from Stack ------------
  248.     push   ebp           ;return address is 4 bytes and this push is 4 bytes
  249.     mov    ebp,esp       ;so first parameter is 8 bytes from top
  250.     mov    eax,[ebp+8]
  251.     mov    parm1,eax
  252.     mov    eax,[ebp+12]
  253.     mov    parm2,eax
  254.     mov    eax,[ebp+16]
  255.     mov    parm3,eax
  256.     mov    eax,[ebp+20]
  257.     mov    parm4,eax
  258.  
  259.     ;---------------- WM_CREATE ----------------
  260.     .IF parm2 == WM_CREATE
  261.          Invoke InitMainWindow,parm1,parm3,parm4
  262.          mov      eax,FALSE  ;return value
  263.     .ENDIF
  264.  
  265.     ;---------------- WM_PAINT ----------------
  266.     .IF parm2 == WM_PAINT ; && parm1 == hwndMain
  267.        Invoke  MainPaint,parm1
  268.        mov      eax,FALSE  ;return value
  269.     .ENDIF
  270.  
  271.     ;---------------- WM_CHAR ----------------
  272.    .IF parm2 == WM_CHAR
  273.       Invoke  MainKeyboard,parm3,parm4,parm1    ;sets return value
  274.    .ENDIF
  275.  
  276.     ;----- Default Procedure * Return Value in eax ------
  277.     $WinDefWindowProc parm1,parm2,parm3,parm4
  278.  
  279.     ;----- Restore Stack Pointer and Stack Status
  280.     mov      esp,ebp    ;restore  stack pointer
  281.     pop      ebp
  282.     ret
  283. MainWinProc  endp
  284.  
  285. ;-------------- MainPaint * WM_PAINT --------------
  286. MainPaint  proc Near SYSCALL uses eax, var1:DWORD   ;var1 = hwnd
  287. ;-------- obtain presentation space handle
  288.       $WinBeginPaint  var1,NULL,offset rect
  289.       mov  hps,eax        ;returns presentation space handle
  290.       $WinQueryWindowRect var1,offset rect
  291.       $WinFillRect hps,offset rect,CLR_YELLOW  ;SYSCLR_WINDOW
  292.       mov   textPos,DT_CENTER
  293.       or    textPos,DT_WORDBREAK
  294.       $WinDrawText  hps,-1,offset textMsg,offset rect,CLR_DARKGREEN,CLR_BLUE,TextPos  ;DT_CENTER
  295.       .IF eax == 0
  296.           $DosBeep 50,2000
  297.       .ENDIF
  298.       $WinEndPaint   hps
  299.       $WinReleasePS hps
  300.       ret
  301. MainPaint  endp
  302.  
  303. ;-------------- InitMainWindow * WM_CREATE --------------
  304. ;var1 = hwnd, var2 = mp1, var3 = mp2
  305. InitMainWindow Proc Near SYSCALL, var1:DWORD,var2:DWORD,var3:DWORD
  306.  
  307.        ret
  308. InitMainWindow Endp
  309.  
  310. ;-------------- MainKeyBoard * WM_CHAR --------------
  311. ;var1 = mp1,var2 = mp2,var3 = hwnd
  312. MainKeyBoard  Proc Near SYSCALL uses ebx ecx,var1:DWORD,var2:DWORD,var3:DWORD
  313.       mov    ebx,mp1    ;will check for char codes
  314.       .IF    bx & KC_CHAR   ;char code valid
  315.           mov    ebx,mp2
  316.          .IF    bx == 'Q'  || bx == 'q'
  317.              $DosBeep 100,2000
  318.          .ELSEIF  bx == 'm'  || bx == 'M'
  319. ;                  $WinErrorMessage msgboxWinChangeSwitchEntry
  320.              $Alarm
  321.              $WinMessageBox HWND_DESKTOP,HWND_DESKTOP,offset msgInitErr,NULL,0,msgBoxStyle
  322.              .IF  eax == MBID_YES
  323.                  $DosBeep  50,1000
  324.              .ELSE
  325.                  $DosBeep 1000,1000
  326.              .ENDIF
  327.          .ENDIF
  328.           mov eax,TRUE     ;message processed
  329.           ret
  330.       .ENDIF
  331.        mov    eax,FALSE    ;not processed
  332.       ret
  333. MainKeyBoard Endp
  334.  
  335. END   startup                    ;required
  336.  
  337.